home *** CD-ROM | disk | FTP | other *** search
/ Total Network Tools 2002 / NextStepPublishing-TotalNetworkTools2002-Win95.iso / Archive / Misc Servers / Zope.exe / GFINTROSPECT.PY < prev    next >
Encoding:
Python Source  |  1999-07-28  |  5.1 KB  |  185 lines

  1. """
  2. View based introspection and extension views
  3. """
  4.  
  5. import gfdb0
  6.  
  7. class RemoteView(gfdb0.View):
  8.  
  9.     """Virtual superclass.  See text for methods and members to define."""
  10.     
  11.     # Usually redefine __init__
  12.     def __init__(self):
  13.         pass
  14.     
  15.     # set static (static=1) or dynamic (static=0)
  16.     # for static tuple seq is generated once per load
  17.     # for non-static tuple seq is regenerated once per query
  18.     #  which uses the view.
  19.     static = 0
  20.     
  21.     # define the column_names
  22.     column_names = ['Column1']
  23.     
  24.     # define the row generator
  25.     def listing(self):
  26.         """return list of values (1 column)
  27.            or list of tuples of values (>1 column).
  28.            size of elts should match number of columns."""
  29.         return [0]
  30.         
  31.     # possibly redefine __repr__ and irepr
  32.     def __repr__(self):
  33.         return "<Remote View %s at %s>" % (self.__class__, id(self))
  34.         
  35.     irepr = __repr__
  36.     
  37.     # for introspective methods possibly redefine relbind
  38.     def relbind(self, db, atts):
  39.         return self
  40.         
  41.     ### don't touch the following unless you are a guru!
  42.     cached_rows = None
  43.     
  44.     def uncache(self):
  45.         if self.static: return
  46.         self.cached_rows = None
  47.         
  48.     def attributes(self):
  49.         from string import upper
  50.         return map(upper, self.column_names)
  51.         
  52.     def rows(self, andseqs=0):
  53.         cached_rows = self.cached_rows
  54.         if cached_rows is None:
  55.             tups = list(self.listing())
  56.             from sqlsem import kjbuckets
  57.             undump = kjbuckets.kjUndump
  58.             attributes = tuple(self.attributes())
  59.             for i in xrange(len(tups)):
  60.                 tups[i] = undump(attributes, tups[i])
  61.             cached_rows = self.cached_rows = tups
  62.         tups = cached_rows[:]
  63.         if andseqs:
  64.             return (tups, range(len(tups)))
  65.         else:
  66.             return tups
  67.  
  68. class DualView(RemoteView):
  69.     """static one row one column view for testing.
  70.        (Inspired by Oracle DUAL relation)."""
  71.     # trivial example extension view
  72.     
  73.     static = 1
  74.     
  75.     column_names = ['Column1']
  76.     
  77.     def listing(self):
  78.         return [0]
  79.         
  80. class DictKeyValueView(RemoteView):
  81.     """Less trivial example. Dict keys/values converted to strings"""
  82.     
  83.     static = 0 # regenerate in case dict changes
  84.     
  85.     column_names = ["key", "value"]
  86.     
  87.     mapstring = 1
  88.     
  89.     def __init__(self, dict=None):
  90.         if dict is None: dict = {}
  91.         self.dict = dict
  92.         
  93.     def listing(self):
  94.         items = self.dict.items()
  95.         if self.mapstring:
  96.             def mapper(item):
  97.                 return tuple(map(str, item))
  98.             return map(mapper, items)
  99.         else:
  100.             return items
  101.         
  102. class RelationsView(DictKeyValueView):
  103.     """list of relations and whether they are views."""
  104.     
  105.     column_names = ["table_name", "is_view"]
  106.     mapstring = 0
  107.     
  108.     def relbind(self, db, atts):
  109.         rels = db.rels
  110.         dict = {}
  111.         for relname in rels.keys():
  112.             dict[relname] = rels[relname].is_view
  113.         self.dict = dict
  114.         return self
  115.         
  116. class IndicesView(DictKeyValueView):
  117.     """list of indices and relations they index"""
  118.     
  119.     column_names = ["index_name", "table_name", "is_unique"]
  120.     
  121.     mapstring = 0
  122.     
  123.     def relbind(self, db, atts):
  124.         rels = db.rels
  125.         dict = {}
  126.         for relname in rels.keys():
  127.             rel = rels[relname]
  128.             if not rel.is_view:
  129.                 index_list = rels[relname].index_list
  130.                 for index in index_list:
  131.                     dict[index.name] = (relname, index.unique)
  132.         self.dict = dict
  133.         return self
  134.         
  135.     def listing(self):
  136.         L = []
  137.         dict = self.dict
  138.         keys = dict.keys()
  139.         for k in keys:
  140.             L.append( (k,) + dict[k] )
  141.         return L
  142.         
  143. class DataDefsView(DictKeyValueView):
  144.     """Data defs (of non-special views) and definition dumps."""
  145.     
  146.     column_names = ["name", "defn"]
  147.     
  148.     mapstring = 1
  149.     
  150.     def relbind(self, db, atts):
  151.         self.dict = db.datadefs
  152.         return self
  153.         
  154. class ColumnsView(RemoteView):
  155.     """table_names and columns therein."""
  156.     
  157.     column_names = ["table_name", "column_name"]
  158.     
  159.     def relbind(self, db, atts):
  160.         rels = db.rels
  161.         pairs = []
  162.         for relname in rels.keys():
  163.             for att in rels[relname].attributes():
  164.                 pairs.append( (relname, att) )
  165.         self.pairs = pairs
  166.         return self
  167.         
  168.     def listing(self):
  169.         return self.pairs
  170.         
  171. class IndexAttsView(ColumnsView):
  172.     """indices and attributes."""
  173.     
  174.     column_names = ["index_name", "column_name"]
  175.     
  176.     def relbind(self, db, atts):
  177.         indices = db.indices
  178.         pairs = []
  179.         for iname in indices.keys():
  180.             for att in indices[iname].attributes():
  181.                 pairs.append( (iname, att) )
  182.         self.pairs = pairs
  183.         return self
  184.         
  185.